using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIScoreControl : MonoBehaviour
{
    Sprite[] spriteParentList;
    Dictionary<string,Sprite> spriteDic;
    public GameObject prefab;
    private void Awake()
    {
        AssetBundle assetBundle = ABManger.LoadFromFile("ground");
        spriteDic = new Dictionary<string, Sprite>();
        spriteParentList =assetBundle.LoadAllAssets<Sprite>();
        foreach (Sprite sprite in spriteParentList)
        {
            spriteDic.Add(sprite.name, sprite);
        }
        ABManger.UnLoad(assetBundle, false,out assetBundle);
        for(int i = 0; i < 5; i++)
        {
            values[i] = 0;
        }
        assetBundle = ABManger.LoadFromFile("ui");
        prefab = assetBundle.LoadAsset<GameObject>("UIScore");
        ABManger.UnLoad(assetBundle, false,out assetBundle);
    }
    private int[] values=new int[5];
    public void Show(int value)
    {
        int top;
        for(top = 0; top < 5; top++)
        {
            values[top] = value % 10;
            //Debug.Log("values["+top+"]:"+values[top].ToString());
            value /= 10;
            if (value == 0) break;
        }
        while (top + 1 > transform.childCount)
        {
            GameObject.Instantiate(prefab, this.transform);
        }
        for(int i = top; i >= 0; i--)
        {
            transform.GetChild(top - i).GetComponent<Image>().sprite = spriteDic[values[i].ToString()];
            
        }
    }
}
